Hi guys, so in this lecture we are going to look at how to work out how many bombs surround a given square. This problem might be best solved by splitting it into two parts.

  1. Create a function that takes position(x,y) on the board and returns all its nieghbours (i.e 8 squares).
  2. Create a function that calls that takes all the (x, y) points and counts up the number of bombs.

for example:

[[_,B,-,-],
 [Y,-,-,-],
 [B,B,X,-],
 [B,-,-,B]]

In the above board, we want X to equal 1 because there are 2 bombs nearby. In the case of Y we want to return 3.

A few hints:

  • Make sure you don't change the square if it is a bomb!
  • How do you want to handle IndexErrors?
  • Don't forget to use those helper functions we defined last time (get_square, set_square).
  • If you get stuck, you may find some finds in the design lecture.

In [29]:
def test():
    """
    Write your tests here...
    
    Example:
    >>> 1 + 1
    2
    
    """
    
    print("Testing Complete")

In [ ]:
def neighbour_squares(x, y, num_rows, num_cols):
    """
    (x, y) 0-based index co-ordinate pair.
    num_rows, num_cols: specifiy the max size of the board
    
    returns all valid (x, y) coordinates from starting position.
    """
    pass
    
def count_bombs(x, y, board):
    """
    returns the number of neighbours of (x,y) that are bombs. Max is 8, min is 0.
    """
    pass
    
test()